<?php
namespace app\exam\model;

use app\exam\model\ExamModel;
use app\exam\model\SubjectModel;
class CandidateModel extends CommonModel
{
    /*
        考生登录
        params:
            user_name  用户登录的名称
            exam_id 参加考试的id信息
        return:
            array [
                'token' => '092483079561585EF6A270CEA01B73B9'
            ]
            boolean false 系统错误
    */
    public function login($user_name = '', $exam_id = 0)
    {
        //检查考试状态是否可用
        $examModel = new ExamModel();
        $examStatus = $examModel->checkExamStatus($exam_id);
        if($examStatus === false){
            return false;
        }

        $date = date("ymd");
        $user_name = $user_name.'|'.$date;

        //获取用户当前token
        $candidateToken = $this->getCandidateToken($user_name);
        if(empty($candidateToken) == false){
            $res = array(
                "token" => $candidateToken
            );
        }else{
            //创建用户信息
            $createCandidate = $this->createCandidateInfo($user_name, $exam_id);

            if($createCandidate){
                //正常返回token信息
                $res = array(
                    "token" => $createCandidate
                );
            }else{
                //没有返回token信息
                $res = false;
            }
        }

        return $res;
    }
    /*
        获取用户考试token信息
        author:JiaXu
        params:
            user_name 用户登录的用户名
        return:
            string token
            boolean false 没有找到用户token
    */
    public function getCandidateToken($user_name = '')
    {
        $Dao = db();
        $sql = 'select candidate_token from candidate where candidate_login_name = ? limit 1';
        $params = array(
            $user_name
        );
        $candidate = $Dao
            ->query($sql, $params);


        return empty($candidate[0]['candidate_token']) == false ? $candidate[0]['candidate_token'] : false;
    }

    /*
        创建用户信息
        author:JiaXu
        params:
            user_name 用户登录的用户名
            exam_id 用户参加的考试id
        return:
            string token
            boolean false 设置失败
    */
    public function createCandidateInfo($user_name = '', $exam_id = ''){
        $candidateDao = db();
        $candidateDao = $candidateDao->table("candidate");

        $data = array();
        $data['candidate_login_name'] = $user_name;
        $data['candidate_token'] = $this->createToken($user_name);
        $data['fk_exam_id'] = intval($exam_id);
        $data['candidate_status'] = 1;
        $data['candidate_createtime'] = date('Y-m-d H:i:s');

        $candidateDao->startTrans();
        $saveCandidate = $candidateDao
            ->insertGetId($data);

        if($saveCandidate === false){
            $candidateDao->rollback();
            DLOG("创建用户信息错误:".$candidateDao->getLastSql(), "error", "db");
            return false;
        }

        $SubjectModel = new SubjectModel();
        $initSubject = $SubjectModel->initExamSubject($data['candidate_token'], 50);
        if(initSubject == false){
            $candidateDao->rollback();
            DLOG("用户初始化课程失败:".$candidateDao->getLastSql(), "error", "db");
            return false;
        }
        
        $candidateDao->commit();
        return $data['candidate_token'];

    }

    /*
        生成用户token
        params
            user_name 登录的用户名
        return:
            string token字符串
    */
    public function createToken($user_name = '')
    {
        $token = md5($user_name.rand(1000,9999));
        return strtoupper($token);
    }

    /*
        通过token获取用户信息
        params:
            candidate_token 用户token信息
        return:
            array [
                'candidate_id' => '1', //用户id
                'candidate_login_name' => '沈家旭|160919', //用户登录名称
                'fk_exam_id' => '1', //参加考试的id
                'candidate_status' => '1' 考试状态
            ]
            boolean false 用户信息不存在
    */
    public function getCandidateInfoByToken($candidate_token = '')
    {
        $candidateInfo = cache('CDDI_'.$candidate_token);
        
        if(empty($candidateInfo) == false){
            return $candidateInfo;
        }else{
            $candidateInfo = $this->getDbCandidateInfoByToken($candidate_token);

            if($candidateInfo === false){
                return false;
            }
            cache('CDDI_'.$candidate_token, json_encode($candidateInfo), 86400);
            return $candidateInfo;
        }
    }

    /*
        通过token在数据库中获取用户信息
        params:
            candidate_token 用户token信息
        return:
            array [
                'candidate_id' => '1', //用户id
                'candidate_login_name' => '沈家旭|160919', //用户登录名称
                'fk_exam_id' => '1', //参加考试的id
                'candidate_status' => '1' 考试状态
            ]
            boolean false 用户信息不存在
    */
    public function getDbCandidateInfoByToken($candidate_token = '')
    {
        $candidateDao = db();

        $sql = 'select candidate_id,candidate_login_name,fk_exam_id,candidate_status,candidate_createtime from candidate where candidate_token = ?';
        $params = array(
            $candidate_token
        );
        $candidateInfo = $candidateDao
            ->query($sql, $params);

        return empty($candidateInfo) == false ? $candidateInfo[0] : false;
    }

}
?>